package com.zline.foodraise.baidu;

import android.content.Context;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.util.LUtil;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.UiThread;

/**
 * Created by heshantao on 15/7/5.
 * 百度定位
 */
@EBean
public class LocationUtil {
    private static final String LOG_TAG = LocationUtil.class.getSimpleName();
    @RootContext
    Context cxt;
    LocationClient client;
    LocationClientOption locationClientOption;
    BDLocationListener listener;

    void initClientOption() {
        locationClientOption = new LocationClientOption();
        locationClientOption.setOpenGps(true);
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//高精度模式
        locationClientOption.setIsNeedAddress(true);//是否显示地址信息
        locationClientOption.setCoorType("bd09ll");//返回的定位结果是百度经纬度
        locationClientOption.setProdName(Constant.APP_NAME);//设置产品名称
    }

    public void setDBLocationListener(BDLocationListener listener) {
        this.listener = listener;

    }

    public void setContext(Context context) {
        if(cxt==null){
            this.cxt=context;
        }

    }

   public void initLocationClient() {
        client = new LocationClient(cxt);
        initClientOption();
        if (client != null) {
            client.setLocOption(locationClientOption);
            client.registerLocationListener(listener);//添加监听器
            LUtil.i(LOG_TAG, "____________start_________");

        }
    }


    //开始定位
    public void startLocation(){
        if(client!=null){
            client.start();//启动SDK
        }
    }

    //请求定位
    public void requestLocation() {
        LUtil.i(LOG_TAG, "____请求定位_____"+client.isStarted());
        if (client != null && client.isStarted()) {
            LUtil.i(LOG_TAG, "启动定位");
            client.requestLocation();
        } else {
            LUtil.i(LOG_TAG, "locClient is null or not started");
        }
    }

    //停止定位
    public void stopLocation() {
        if (client != null && client.isStarted()&& listener!=null) {
            client.unRegisterLocationListener(listener);
            client.stop();
        }
    }
}
